﻿Imports System.Runtime.CompilerServices

Namespace Xhtml
    Public Module SelectExtensions

        <Extension()> _
        Public Function DropDownList(ByVal xHelper As XhtmlHelper, _
                                     ByVal name As String, _
                                     ByVal SelectOptions As IDictionary(Of Object, String)) As XElement
            Return DropDownList(xHelper, name, SelectOptions, Nothing, Nothing)
        End Function

        <Extension()> _
        Public Function DropDownList(ByVal xHelper As XhtmlHelper, _
                                     ByVal name As String, _
                                     ByVal SelectOptions As IDictionary(Of Object, String), _
                                     ByVal SelectedKey As Object) As XElement
            Return DropDownList(xHelper, name, SelectOptions, SelectedKey, Nothing)
        End Function

        <Extension()> _
        Public Function DropDownList(ByVal xHelper As XhtmlHelper, _
                                     ByVal name As String, _
                                     ByVal SelectOptions As IDictionary(Of Object, String), _
                                     ByVal SelectedKey As Object, _
                                     ByVal htmlAttributes As Object) As XElement
            Return DropDownList(xHelper, name, SelectOptions, SelectedKey, New RouteValueDictionary(htmlAttributes))
        End Function

        <Extension()> _
        Public Function DropDownList(ByVal xHelper As XhtmlHelper, _
                                     ByVal name As String, _
                                     ByVal SelectOptions As IDictionary(Of Object, String), _
                                     ByVal SelectedKey As Object, _
                                     ByVal htmlAttributes As IDictionary(Of String, Object)) As XElement
            Dim result = <select name=<%= name %>/>
            Dim optElement As XElement
            For Each opt In SelectOptions
                optElement = <option value=<%= opt.Key.ToString %>><%= opt.Value %></option>
                If opt.Key.Equals(SelectedKey) Then
                    optElement.@selected = "1"
                End If
                result.Add(optElement)
            Next
            If htmlAttributes IsNot Nothing Then
                For Each attr In htmlAttributes
                    result.SetAttributeValue(attr.Key, attr.Value)
                Next
            End If
            Return result
        End Function

    End Module
End Namespace